<?php
class UsersController extends Zend_Controller_Action
{
	
	
	function init() 
	{
		// Initialize the view
		$this->initView();
		// Store the base url for the current request in the view
		$this->view->baseUrl = $this->_request->getBaseUrl();	
	}
	
	
	function indexAction()
	{
		$users = new Users();
		$this->view->users = $users->fetchAll();
		

		$user = $this->view->users->getRow(1);
		$this->view->chars = $user->findCharacters();

	}

	function adminDeleteAction()
	{
		$id = (int)$this->_request->getParam('id');
	}
	
	function deleteAction()
	{

	}
	
	
	function createAction()
	{
		$form = new Forms_CreateUserForm();
		
		if ($this->_request->isPost()) {
        	$formData = $this->_request->getPost();
            if ($form->isValid($formData)) {                            
                $users = new Users();                
                $data = array(	'username' 		=> $formData['userName'],
                				'email' 		=> $formData['email'],
								'nickname'		=> $formData['nickName'],
								'password'		=> sha1($formData['password']),
                				'creation_date' => date("Y-m-d H:i:s", time()));
                
				$key = $users->insert($data);

				$this->view->success = true;
				
            } else {
            	$form->populate($formData);
            }
        }
		
		$this->view->form = $form;
	}

	/*Simple version of the users.createAction without using Zend.Form*/
	/*function createAction()
	{
		$this->view->message = '';		
		if ($this->_request->isPost()) {			
		
			$users = new Users();
		
			$name = $this->_request->getParam('username');
			$nickname = $this->_request->getParam('nickname');
			$password = sha1($this->_request->getParam('password'));
		
			$data = array(
				'username' 	=> $name,
				'nickname'	=> $nickname,
				'password'	=> $password
			);
			$users->insert($data);	
		}
		$this->view->title = "Create User";	
	}*/

}
?>